package com.heima.search.listener;

import com.alibaba.fastjson.JSON;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.mess.CreateArticleMess;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-10-27 11:46
 */
@Component
@Slf4j
public class ArticleKafkaListener {

    @Autowired
    private SearchService searchService;

    @KafkaListener(topics = {WmNewsMessageConstants.ARTICLE_CREATE_TOPIC})
    public void createArticle(String msg){
        if(StringUtils.isBlank(msg)){
            log.error("接受创建文章消息为空");
            return;
        }
        CreateArticleMess mess = JSON.parseObject(msg,CreateArticleMess.class);
        SearchArticleVo searchArticleVo = new SearchArticleVo();
        BeanUtils.copyProperties(mess,searchArticleVo);

        searchService.createArticle(searchArticleVo);
    }
}
